package com.project.beagentleman;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
import android.app.Activity;
import android.content.Context;

public class ImageActivity extends Activity implements ViewFactory {

	public static int lessonNumber = 0;
	public static String lessonType = "";
	
	private List<Integer> images;
	
	private ImageSwitcher imageSwitcher;
	private Gallery gallery;
	
	private Context context;
	
	private List<String> listRaws;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_image);

		initComponents();
	}

	public void initComponents() {
		context = this;
		
		images = new ArrayList<Integer>();
		
		listRaws = new ArrayList<String>();
		listRaw();
		getActualRaws();
		
		imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
		imageSwitcher.setFactory(this);
		imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
		imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
		
		gallery = (Gallery) findViewById(R.id.gallery);
		gallery.setAdapter(new ImageAdapter());

		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				imageSwitcher.setImageResource(images.get(arg2));
			}
		});
	}
	
	public void getActualRaws() {
		Pattern pattern = Pattern.compile("l" + lessonNumber + "_" + lessonType + "_");
		Matcher matcher;
		
		for (String raw : listRaws) {
			matcher = pattern.matcher(raw);
			if (matcher.find()) {
				images.add(getResources().getIdentifier(raw, "raw", getApplication().getPackageName()));
			}
		}
	}
	
	public void listRaw() {
	    Field[] fields = R.raw.class.getFields();
	    for (int count = 0; count < fields.length; count++){
	    	listRaws.add(fields[count].getName());
	    }
	}

	public class ImageAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return images.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(context);
			imageView.setImageResource(images.get(position));
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setLayoutParams(new Gallery.LayoutParams(200, 150));

			return imageView;
		}
	}

	@Override
	public View makeView() {
		ImageView imageView = new ImageView(this);
		imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
		imageView.setBackgroundColor(R.xml.background_color);

		return imageView;
	}

}
